package com.aoyy.service;

import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.aoyy.model.User;
import com.aoyy.model.SignInLog;
import com.aoyy.dao.UserDao;
import com.aoyy.dao.SignInLogDao;
import com.aoyy.constant.Constants;
/**=======================================================================================================
 * Service class <code>UserService</code> serves as a user management server.
 * Its goals are to query user, login, register user, lock/unlock user, and so on. 
 * @author this author
 * @date 2017-08-06
 * ======================================================================================================== */
@Service
public class UserService {
	@Autowired
	private UserDao userDao;
	@Autowired
	private SignInLogDao signInLogDao;
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public UserDao getUserDao() {
		return userDao;
	}
	
	public void setSignInLogDao(SignInLogDao signInLogDao) {
		this.signInLogDao = signInLogDao;
	}
	
	public SignInLogDao getSignInLogDao() {
		return signInLogDao;
	}
	
	public void save(User user) {
		userDao.save(user);
	}
	
	public User queryUserByUserName(String userName) {
		return userDao.queryUserByUserName(userName);
	}
	
	public User queryUserByUserId(int userId) {
		return userDao.queryUserByUserId(userId);
	}
	
	// @see com.aoyy.dao.UserDao
	public List<User> queryAllUsers() {
		return userDao.queryAllUsers();
	}
	
	public void lockUser(String userName) {
		User user = userDao.queryUserByUserName(userName);
		user.setLocked(Constants.USER_LOCKED);
		userDao.lockUser(userName);
	}
	
	public void unlockUser(String userName) {
		User user = userDao.queryUserByUserName(userName);
		user.setLocked(Constants.USER_UNLOCKED);
		userDao.unlockUser(userName);
	}
	
	public void updateAfterSignIn(User user) {
	    userDao.upateUserAfterSignIn(user);
	    SignInLog signInLog = new SignInLog();
	    signInLog.setIp(user.getLastIp());
	    signInLog.setCreateTime(user.getLastVisit());
	    signInLog.setUser(user);
	    signInLogDao.save(signInLog);			
	}
	
	// @see com.aoyy.dao.UserDao
	public void updateUserCreditOfDigestTopic(Integer userId, Integer isDigestTopic) {
		userDao.updateUserCreditOfDigestTopic(userId, isDigestTopic);
	}
	
	// @see com.aoyy.dao.UserDao
	public void updateUserCreditByEmitOrRemoveTopic(Integer userId, Integer emitOrRemoveTopic) {
		userDao.updateUserCreditByEmitOrRemoveTopic(userId, emitOrRemoveTopic);
	}
	
	// @see com.aoyy.dao.UserDao
	public void updateUserCreditByEmitOrRemovePost(Integer userId, Integer emitOrRemovePost) {
		userDao.updateUserCreditByEmitOrRemovePost(userId, emitOrRemovePost);
	}

}
